﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;

namespace YunCMS.Web.Admin.User
{
    public partial class EditUser : System.Web.UI.Page
    {
        private YCUserBLL _bll = null;

        protected  YCUserBLL BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCUserBLL();
                }
                return _bll;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = Request.QueryString["action"];
                int id;
                BindDDL();
                if (!string.IsNullOrEmpty(action) && action == "edit")
                {
                    string _id = Request.QueryString["id"];

                    if (!int.TryParse(_id, out id))
                        id = 0;

                    ViewState["ID"] = id;
                    BindControls(id);

                    saveBt.Text = "更新";
                }
                else
                {
                    saveBt.Text = "新增";
                }
            }
        }

        private void BindDDL()
        {
            YCUserRoleBLL bll = new YCUserRoleBLL();
            IList<YCUserRole> list = bll.GetList();

            ddlRoleId.DataSource = list;
            ddlRoleId.DataTextField = "Name";
            ddlRoleId.DataValueField = "ID";
            ddlRoleId.DataBind();
        }

        private void BindControls(int id)
        {
            if (BLL.IsExist(id))
            {
                YCUser model = BLL.GetYCUser(id);
                FillControls(model);
            }
            else
            {
                JScript.Alert("不存在该条记录", this);
            }
        }

        private void FillControls(YCUser model)
        {
            txtUserName.Text = model.UserAccount.ToString();
            txtUserPwd.Text = YunCMSUtil.DecryptPwd(model.Password.ToString());
            txtEmail.Text = model.Email.ToString();
            txtTelephone.Text = model.Telephone.ToString();
            txtRealName.Text = model.Name.ToString();
            rblIsLock.SelectedValue = model.Deactive.ToString();

            txtMemo.Text = model.OtherInfo.ToString();
            if (model.Role.HasValue)
                ddlRoleId.SelectedValue = model.Role.Value.ToString();
        }

        private void FillModel(YCUser model)
        {

            model.UserAccount = txtUserName.Text.Trim();
            model.Password = YunCMSUtil.EncryptPwd(txtUserPwd.Text.Trim());
            model.Email = txtEmail.Text.Trim();
            model.Telephone = txtTelephone.Text.Trim();
            model.Name = txtRealName.Text.Trim();
            model.Deactive = rblIsLock.Items[0].Selected;

            model.OtherInfo = txtMemo.Text.Trim();
            if (ddlRoleId.SelectedValue.Trim() != "")
                model.Role = System.Convert.ToInt32(ddlRoleId.SelectedValue.Trim());
            else
                model.Role = null;
        }

        protected void saveBt_Click(object sender, EventArgs e)
        {
            YCUser model;
            int i = 0, id;
            string action = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(action) && action == "edit")
            {
                if (ViewState["ID"] != null)
                {
                    id = System.Convert.ToInt32(ViewState["ID"]);
                    model = BLL.GetYCUser(id);
                    FillModel(model);
                    i = BLL.Update(model);
                }
            }
            else
            {
                model = new YCUser();
                FillModel(model);
                i = BLL.Add(model);
            }

            if (i > 0)
            {
                Response.Redirect("UserList.aspx");
            }
        }
    }
}